//
//  CInputSystem.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by bk on 13/1/6.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "CInputSystem.h"

void CInputSystem::Init()
{
    
}


void CInputSystem::AddListen(IInputListen* ptr)
{
    m_vInputListen.push_back(ptr);
}


void CInputSystem::RemoveAllListen()
{
    m_vInputListen.clear();
}

void CInputSystem::AddAction(CInputAction* ptr)
{
    //m_vInputAction.push_back(ptr);
    
    m_mapInputAction[ptr->GetInputMapping()->m_inputID] = ptr;
}

void CInputSystem::RemoveAllAction()
{
    map<INPUT_ID,CInputAction*>::iterator pi = m_mapInputAction.begin();
    
    while (pi != m_mapInputAction.end())
    {
        delete pi->second;
        
        pi->second = NULL;
        
        pi++; //= m_mapInputAction.erase(pi);
    }
}

void CInputSystem::Update()
{
    map<INPUT_ID,CInputAction*>::iterator pi = m_mapInputAction.begin();
    
    while (pi != m_mapInputAction.end())
    {
        if(pi->second->isTriggered())
        {
            vector<IInputListen*>::iterator ppi = m_vInputListen.begin();
            
            while (ppi != m_vInputListen.end())
            {
                (*ppi)->HandleInput(pi->second->GetInputMapping());
                
                ppi++;
            }
            
            pi->second->UnTriggered();
        }
        
        pi++;
    }
}

CInputAction* CInputSystem::GetInputAction(INPUT_ID inputID)
{
    return m_mapInputAction[inputID];
}

